package jgl.vajra.operator.widget.utils;

import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;

import jgl.vajra.operator.R;

import jgl.vajra.operator.constant.Constant;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.MultiTypeBindingAdapter;
import jgl.vajra.operator.widget.giide.GlideImageLoader;
import jgl.vajra.operator.widget.loopviewpage.AutoSwitchView;
import jgl.vajra.operator.widget.manager.BannerManager;
import jgl.vajra.operator.widget.view.NumberRunningTextView;
import jgl.android.loadinglibrary.LoadingIndicatorView;
import jgl.android.loadinglibrary.indicators.BallSpinFadeLoaderIndicator;

import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2017/2/9 0009.
 */

public class DataBindingUtils {

    @BindingAdapter("adapter")
    public static void setAdapter(RecyclerView rv, RecyclerView.Adapter adapter) {
        rv.setAdapter(adapter);
    }

    @BindingAdapter("layoutManager")
    public static void setLayoutManager(RecyclerView rv, RecyclerView.LayoutManager layoutManager) {
        rv.setLayoutManager(layoutManager);
    }

    @BindingAdapter("onDisplayImage")
    public static void onDisplayImage(ImageView iv, String url) {
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        if (TextUtils.isEmpty(url)) return;
        if (url.startsWith("http")) {
            GlideImageLoader.onDisplayImage(iv, url);
        } else {
            GlideImageLoader.onDisplayImage(iv.getContext(), iv, new File(url));
        }
    }


    @BindingAdapter("onBigGoodsImageDisplay")
    public static void onBigGoodsImageDisplay(ImageView iv, String url) {
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        GlideImageLoader.onBigGoodsImageDisplay(iv, url);
    }


    @BindingAdapter("onDisplayImage")
    public static void onDisplayImage(ImageView iv, int url) {
        iv.setImageResource(url);
    }

    @BindingAdapter("onDisplayImageRadius")
    public static void onDisplayRadius(ImageView iv, String url) {
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        GlideImageLoader.onDisplayRadius(iv, url, R.dimen.dp_5);
    }

    @BindingAdapter("onDisplayImageHeightRadius")
    public static void onDisplayImageHeightRadius(ImageView iv, String url) {
        GlideImageLoader.onDisplayImageHeightRadius(iv, url, DisplayUtils.getDimension(R.dimen.dp_163), DisplayUtils.getDimension(R.dimen.dp_91));
    }

    @BindingAdapter("onDisplayImageHeight66Radius")
    public static void onDisplayImageHeight66Radius(ImageView iv, String url) {
        GlideImageLoader.onDisplayImageHeightRadius(iv, url, DisplayUtils.getDimension(R.dimen.dp_120), DisplayUtils.getDimension(R.dimen.dp_66));
    }

    @BindingAdapter("onDisplayImageTopRadius")
    public static void onDisplayTopRadius(ImageView iv, String url) {
        GlideImageLoader.onDisplayTopRadius(iv, url, R.dimen.dp_5);
        // GlideImageLoader.onBannerDisplayImage(iv, url );
    }

    @BindingAdapter("onImageRes")
    public static void onImageRes(ImageView iv, int url) {
        iv.setImageResource(url);
    }

    @BindingAdapter("onBookTypeImage")
    public static void onBookTypeImage(ImageView iv, int type) {
        //	资源类型 1：图书 2：文章 4：有声书

        switch (type) {
            case Constant.BOOk_TYPE.TYPE_VIDEO:
                iv.setImageResource(R.drawable.ic_mine_follow_details_video);
                break;
            case Constant.BOOk_TYPE.TYPE_EBOOK:
                iv.setImageResource(R.drawable.ic_mine_follow_details_book);
                break;
            case Constant.BOOk_TYPE.TYPE_ARTICLE:
                iv.setImageResource(R.drawable.ic_mine_follow_details_book_article);
                break;
        }

    }


    @BindingAdapter("onAvatarDisplayRound")
    public static void onAvatarDisplayRound(ImageView iv, String url) {
        if (!TextUtils.isEmpty(url)) {
            GlideImageLoader.onAvatarDisplayRound(iv, url, null);
        } else {
            iv.setImageResource(R.drawable.ic_mine_touxiang_default);
        }
    }

    @BindingAdapter("initLoadingIndicatorView")
    public static void initLoadingIndicatorView(LoadingIndicatorView loadingView, @ColorRes int color) {
        loadingView.setIndicatorColor(color);
        loadingView.setIndicator(new BallSpinFadeLoaderIndicator());
    }

    @BindingAdapter({"android:onClick", "android:clickable"})
    public static void setOnClick(View view, View.OnClickListener clickListener,
                                  boolean clickable) {
        setOnClick(view, clickListener);
        view.setClickable(clickable);
    }


    @BindingAdapter({"android:onClick"})
    public static void setOnClick(View view, final View.OnClickListener clickListener) {
        final long[] mHits = new long[2];
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();
                if (mHits[0] < (SystemClock.uptimeMillis() - 300)) {
                    clickListener.onClick(v);
                } else {
                    Logger.d("重复点击");
                }
            }
        });
    }

    @BindingAdapter("initBanner")
    public static void initBanner(AutoSwitchView view, int type) {
        BannerManager.getInstance().setBannerDataWithType((AppCompatActivity) view.getContext(), type, view);
    }

    @BindingAdapter("addTextBold")
    public static void addTextBold(TextView textView, boolean isSelect) {
        TextPaint paint = textView.getPaint();
        paint.setFakeBoldText(isSelect);
    }


    @BindingAdapter("addTextViewUnderline")
    public static void addTextViewUnderline(TextView textView, boolean isSelect) {
        textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        textView.getPaint().setAntiAlias(true);//抗锯齿
    }

    @BindingAdapter("setImageViewSelect")
    public static void setImageViewSelect(View textView, boolean isSelect) {
        textView.setSelected(isSelect);
    }


    @BindingAdapter(value = {"initRecyclerViewData", "initRecyclerViewPresenter", "initRecyclerViewLayoutManager", "initRecyclerViewItemLayout"}, requireAll = false)
    public static void initRecyclerView(RecyclerView view, List list, BaseBindingItemPresenter presenter, RecyclerView.LayoutManager layoutManager, int itemLayout) {
        MultiTypeBindingAdapter adapter = new MultiTypeBindingAdapter(view.getContext(), list, itemLayout);
        adapter.setItemPresenter(presenter);
        view.setLayoutManager(layoutManager);
        view.setAdapter(adapter);
    }

    @BindingAdapter("onDisplayRoundFile")
    public static void onDisplayRoundFile(ImageView iv, String url) {
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("http")) {
                GlideImageLoader.onDisplayRound(iv.getContext(), iv, url);
            } else {
                GlideImageLoader.onDisplayRound(iv.getContext(), iv, new File(url));
            }
        }
    }

    @BindingAdapter("runtext")
    public static void setRunText(NumberRunningTextView _view, String text) {
        if (!TextUtils.isEmpty(text)) {
            _view.setContent(text);
        }
    }

    @BindingAdapter("onImageViewSelect")
    public static void onImageViewSelect(ImageView imageView, boolean b) {
        imageView.setSelected(b);
    }

    @BindingAdapter(value = {"replaceStringRes", "replaceStringText"}, requireAll = false)
    public static void onImageViewSelect(TextView textView, String res, Object o) {
        //textView.setText(String.format(UIUtils.getString(res),o));
    }

    @BindingAdapter("onTextViewStrikeThru")
    public static void onTextViewStrikeThru(TextView textView, Object res) {
        textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
    }

    @BindingAdapter("onTextMessageCount")
    public static void onTextMessageCount(TextView textView, int res) {
        CommonUtils.setTextMessageCount(textView, res);
    }





    @BindingAdapter("onChoiceQuestionCheckStatusBackground")
    public static void onChoiceQuestionCheckStatusBackground(TextView textView, int status) {
        if (status == 1) {
            textView.setBackgroundResource(R.drawable.shape_item_answer_bg_select);
        } else if (status == 2) {
            textView.setBackgroundResource(R.drawable.shape_item_answer_bg_error);
        } else {
            textView.setBackgroundResource(R.drawable.shape_item_answer_bg_nor);
        }
    }

    @BindingAdapter("onChoiceQuestionTypeText")
    public static void onChoiceQuestionTypeText(TextView textView, int choiceType) {
        if (choiceType == 0) return;
        String text = getChoiceQuestionTypeText(choiceType);
        int res = 0;
        int textColor = 0;
        if (choiceType == Constant.QUESTION_CHOICE_TYPE.SINGLE) {
            res = R.drawable.shape_choice_question_single;
            textColor = R.color.ff9312;
        } else if (choiceType == Constant.QUESTION_CHOICE_TYPE.MULTIPLE) {
            res = R.drawable.shape_choice_question_uncertain;
            textColor = R.color.blue_4794fa;
        } else if (choiceType == Constant.QUESTION_CHOICE_TYPE.INDEFINITE) {
            res = R.drawable.shape_choice_question_multi;
            textColor = R.color.ff4141;
        }
        Logger.d(" onChoiceQuestionTypeText choiceType" + choiceType);

        Logger.d(" onChoiceQuestionTypeText textColor" + textColor);
        textView.setText(text);
        textView.setTextColor(UIUtils.getColor(textColor));
        textView.setBackgroundResource(res);
        // android:background="@drawable/shape_choice_question_single"

    }



    private static String getChoiceQuestionTypeText(int choiceType) {
        String text = "";
        if (choiceType == Constant.QUESTION_CHOICE_TYPE.SINGLE) {
            text = "单选题";
        } else if (choiceType == Constant.QUESTION_CHOICE_TYPE.MULTIPLE) {
            text = "多选题";
        } else if (choiceType == Constant.QUESTION_CHOICE_TYPE.INDEFINITE) {
            text = "不定项";
        }
        return text;
    }

    @BindingAdapter(value = {"onQuestionType", "onQuestionDifficulty", "onQuestionChoiceType"}, requireAll = false)
    public static void onQuestionDifficulty(TextView textView, int questionType, int difficulty, int choiceType) {
        String text = "";
        if (questionType == 1) {
            //选择题类型（1:单选题 2:多选题 3:不定项选择）
            text = getChoiceQuestionTypeText(choiceType);
        } else if (questionType == 2) {
            if (difficulty == 1) {
                text = "简单";
            } else if (difficulty == 2) {
                text = "中等";
            } else if (difficulty == 3) {
                text = "困难";
            }
        }
        int res = R.drawable.shape_f5f5f7_4dp;
        int textColor = 0;
        int type = questionType == 1 ? choiceType : difficulty;
        if (type == 1) {
            textColor = R.color.ff9312;
        } else if (type == 2) {
            textColor = R.color.blue_4794fa;
        } else if (type == 3) {
            textColor = R.color.ff4141;
        }
        textView.setText(text);
        textView.setTextColor(UIUtils.getColor(textColor));
        textView.setBackgroundResource(res);
    }

    @BindingAdapter(value = {"onDownloadStatusText", "onDownloadErrorStatus"}, requireAll = false)
    public static void onDownloadStatusText(TextView textView, int downloadStatus, int errorStatus) {
        switch (downloadStatus) {
            case Constant.DOWNLOAD_STATUS.UN_START:
            case Constant.DOWNLOAD_STATUS.SUCCESS:
                textView.setText(" ");
                break;
            case Constant.DOWNLOAD_STATUS.PENDING:
                textView.setText("等待中");
                break;
            case Constant.DOWNLOAD_STATUS.STOP:
                textView.setText("已暂停");
                break;
            case Constant.DOWNLOAD_STATUS.DOWNLOADING:
                textView.setText("下载中");
                break;
            case Constant.DOWNLOAD_STATUS.ERROR:
                if (errorStatus == 1) {
                    textView.setText("文件被删除");
                } else {
                    textView.setText("下载错误");
                }
                break;
        }

    }




    @BindingAdapter(value = {"onCoursePrice", "onCourseDiscountPrice"}, requireAll = false)
    public static void onCourseDiscountPrice(TextView textView, double price, double discountPrice) {
        if (price <= 0) {
            textView.setText("免费");
            textView.setTextColor(UIUtils.getColor(R.color.gray_66));
        } else {
            if (discountPrice > 0) {
                textView.setTextColor(UIUtils.getColor(R.color.red_c700));
                textView.setText(String.format(UIUtils.getString(R.string.money_double), discountPrice));
            } else {
                textView.setTextColor(UIUtils.getColor(R.color.orange_f183));
                textView.setText(String.format(UIUtils.getString(R.string.money_double), price));

            }
        }
    }

    @BindingAdapter(value = {"onProgressBarMax", "onProgressProgress"}, requireAll = false)
    public static void onProgressBarData(ProgressBar progressBar, long progressBarMax, long progressBarProgress) {
        if (progressBarProgress > 0) {
            int progress = (int) (progressBarProgress * 100 / progressBarMax);
            progressBar.setProgress(progress);
        }
    }



    @BindingAdapter(value = {"onCourseLiveStateView"}, requireAll = false)
    public static void onCourseLiveStateView(View view, int liveStatus) {
        if (liveStatus == Constant.COURSE_LIVE_STATUS.UNSTART) {
            view.setVisibility(View.VISIBLE);
            Drawable drawable = UIUtils.getContext().getResources().getDrawable(R.drawable.shape_live_nozb_bg);
            view.setBackground(drawable);
        } else if (liveStatus == Constant.COURSE_LIVE_STATUS.LIVEING) {
            Drawable drawable = UIUtils.getContext().getResources().getDrawable(R.drawable.shape_live_zb_bg);
            view.setVisibility(View.VISIBLE);
            view.setBackground(drawable);
        } else if (liveStatus == Constant.COURSE_LIVE_STATUS.END
                || liveStatus == Constant.COURSE_LIVE_STATUS.RESETPLAY) {
            view.setVisibility(View.GONE);
        }
    }

    @BindingAdapter(value = {"onCommentTypeText"}, requireAll = false)
    public static void onCommentTypeText(TextView textView, int commentType) {
        if (commentType == Constant.COMMENT_TYPE.REVIEW_HOMEWORK) {
            textView.setText("点评了你的作业");
        } else if (commentType == Constant.COMMENT_TYPE.REPLY_HOMEWORK) {
            textView.setText("回复了你的作业");
        } else if (commentType == Constant.COMMENT_TYPE.COMMENT_ANSWER_KEY) {
            textView.setText("评论了你的题解");
        } else if (commentType == Constant.COMMENT_TYPE.COMMENT_REVIEWS_KEY) {
            textView.setText("回复了你的题目评论");
        } else {
            textView.setText("");
        }
    }

    @BindingAdapter(value = {"onLikeTypeText"}, requireAll = false)
    public static void onLikeTypeText(TextView textView, int likeType) {
        if (likeType == Constant.LIKE_TYPE.LIKE_HOMEWORK) {
            textView.setText("点赞了你的作业");
        } else if (likeType == Constant.LIKE_TYPE.LIKE_QUESTION_BAND_ANSWER_REVIEWS) {
            textView.setText("点赞了你的题解评论");
        } else if (likeType == Constant.LIKE_TYPE.LIKE_HOMEWORK_ANSWER) {
            textView.setText("点赞了你的作业评论");
        } else if (likeType == Constant.LIKE_TYPE.LIKE_QUESTION_BAND_REVIEWS) {
            textView.setText("点赞了你的题目评论");
        } else if (likeType == Constant.LIKE_TYPE.LIKE_QUESTION_BAND_ANSWER_KEY) {
            textView.setText("点赞了你的题解");
        } else if (likeType == Constant.LIKE_TYPE.LIKE_QUESTION_BAND_REVIEWS_DETAILS) {
            textView.setText("点赞了你的题目评论的回复");
        } else {
            textView.setText("");
        }
    }

    @BindingAdapter(value = {"onMessageNoticeCountText"}, requireAll = false)
    public static void onMessageNoticeCountText(TextView textView, int count) {
        if (count > 0) {
            textView.setText("" + count);
            if (count > 99) {
                textView.setText(99 + "+");
            }
        } else {
            textView.setVisibility(View.GONE);
        }
    }


    @BindingAdapter(value = {"onMessageNoticeTimeText"}, requireAll = false)
    public static void onMessageNoticeTimeText(TextView textView, String time) {
        if (TextUtils.isEmpty(time)) {
            textView.setVisibility(View.GONE);
            return;
        }
        textView.setVisibility(View.VISIBLE);

        String s = LongTimeUtil.getTime(Long.parseLong(LongTimeUtil.stampToDate(time)));
        textView.setText(s);
    }


}
